package com.sp.homework;

import java.io.*;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;

public class Homework01Client {
    public static void main(String[] args) throws IOException {
        //1.连接本机的9999端口, 如果连接成功, 返回socket对象
        Socket socket = new Socket(InetAddress.getLocalHost(), 9999);
        System.out.println("客户端socket返回 = " + socket.getClass());

        OutputStream outputStream = socket.getOutputStream();
        BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream));

        InputStream inputStream = socket.getInputStream();
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));

        Scanner scanner = new Scanner(System.in);
        boolean flag = true;
        while (flag) {
            String next = scanner.next();
            if (next.equals("q")) {
                flag = false;
            }
            bufferedWriter.write(next);
            bufferedWriter.newLine();
            bufferedWriter.flush();

            String s = bufferedReader.readLine();
            System.out.println(s);
        }


        bufferedWriter.close();
        bufferedReader.close();
        socket.close();
        System.out.println("客户端退出");


    }
}
